package changceilingcolor003;

import java.util.ArrayList;

import toxi.geom.Vec3D;

public class World {

	ChangCeilingColor003 p5;
	ArrayList<Actors> m_AllAttraction = null;
	

	ArrayList<Actors> GetAttractionList()
	{
		return 	m_AllAttraction;
	}
	World(ChangCeilingColor003 _p5) {
		p5 = _p5;		
	}

	//setup 
	void init() {
		if(p5.Vistor){
		deployPerson(p5.pop*p5.Capacity_1/100, 1);
		}
		if(p5.Artist){
		deployPerson(p5.pop*p5.Capacity_2/100, 2);
		}
		if(p5.Dealer){
		deployPerson(p5.pop*p5.Capacity_3/100, 3);
		}
	}
	//draw
	void run() {
		runPeople();
	}

	void runPeople() {
		
		
		
			
		p5.AllActiveActors.clear();
		//p5.AllActiveActors1.clear();
		//p5.AllActiveActors2.clear();
		//p5.AllActiveActors1.addAll(p5.alisDst);
		//p5.AllActiveActors2.addAll(p5.alisPeop);
		p5.AllActiveActors.addAll(p5.alisDst);
		p5.AllActiveActors.addAll(p5.alisPeop);
		


		if(!p5.PausePeople)
		{

			///update the people state and deploy new one if needed
			for (int i=0;i<p5.alisPeop.size();i++) {
				Person p=(Person)p5.alisPeop.get(i); 			//
				if (p.there==true) {
					p5.alisPeop.remove(i);
					deployPerson(1, p.type);
				}
				else {
					p.update(this);   
				}
			}
		}		
		for (int i=0;i<p5.alisDst.size();i++) {
			Attraction attarction=(Attraction )p5.alisDst.get(i);
			attarction.update(this);		
		}
		
		//if(p5.ChangeActor==true){
			//for (int i =0; i<p5.alisUnit.size(); ++i){
				//Creator unit =  (Creator) p5.alisUnit.get(i);
			//}
		//}
	}

	void deployPerson(int pop, int type) {
		for (int i=0;i<pop;i++) {
			Vec3D oS=(Vec3D)p5.alisOrigin.get((int)p5.random(0, p5.alisOrigin.size()));//origin points are imported from Rhino/now is randomly in order/we need to define the order if we need--------------------------------------
			Vec3D oStart=oS.copy();
			Vec3D oEnd ;
			do{
				oEnd       =(Vec3D)p5.alisOrigin.get((int)p5.random(0, p5.alisOrigin.size()));//destination
			}while((oEnd.x == oStart.x) || (oEnd.y == oStart.y));

			//Vec3D oEnd=(Vec3D)alisOrigin.get((int)random(0, alisOrigin.size()));//destination
			
			Destination destination = new Destination (p5,oEnd); 
			destination.SetLoctation(oEnd);
			Person p=new Person(p5,oStart, destination, type);//Vec3D pos, Vec3D destiny, int type
			p5.alisPeop.add(p);
		}
	}


}
